﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using UDS.Components;

namespace UDS.SubModule.WorkAttendance
{
    /// <summary>
    /// StaffData 的摘要说明。
    /// </summary>
    public partial class StaffDataPage : Page
    {
        protected static string GetMemo(object id)
        {
            if (id.ToString() == "")
            {
                return "";
            }
            var db = new Database();
            SqlParameter[] prams = {
                Database.MakeInParam("@id", SqlDbType.BigInt, 8, Int64.Parse(id.ToString())),
                Database.MakeOutParam("@result", SqlDbType.NVarChar, 300)
            };
            db.RunProc("dbo.sproc_WA_GetMemo", prams);
            return (prams[1].Value.ToString());
        }

        protected static string GetTime(object day)
        {
            if (day.ToString() == "")
            {
                return "";
            }
            return (DateTime.Parse(day.ToString()).ToShortTimeString());
        }

        protected override void OnLoad(EventArgs e)
        {
            DateTime beginTime;
            DateTime endTime;
            int staffId;
            if (!DateTime.TryParse(Request.QueryString["begintime"], out beginTime))
            {
                beginTime = DateTime.Now;
            }
            if (!DateTime.TryParse(Request.QueryString["endtime"], out endTime))
            {
                endTime = DateTime.Now;
            }
            if (!int.TryParse(Request.QueryString["staffid"], out staffId))
            {
                staffId = 0;
            }
            string filter = "";
            if (!Page.IsPostBack)
            {
                var type = (Request.QueryString["type"] == null) ? DateTime.Now.ToString() : Request.QueryString["type"];
                switch (type)
                {
                    case "1":
                        type = "正常上班";
                        filter = "OnDuty_Status = false and OffDuty_Status = false";
                        break;
                    case "2":
                        type = "迟到";
                        filter = "OnDuty_Status = true";
                        break;
                    case "3":
                        type = "早退";
                        filter = "OffDuty_Status = true";
                        break;
                    case "4":
                        type = "未考勤";
                        break;
                    case "5":
                        type = "总考勤";
                        break;
                }

                var db = new Database();

                //得到用户名
                SqlParameter[] pram = {
                    Database.MakeInParam("@ids", SqlDbType.NVarChar, 1000, staffId)
                };
                using (SqlDataReader dr1 = db.RunDataReader("dbo.sproc_WA_GetSelectedStaffFromID", pram))
                {
                    while (dr1.Read())
                    {
                        ltlname.Text = dr1["staff_name"].ToString();
                    }
                }

                if (type == "未考勤")
                {
                    var prams = new[]
                    {
                        Database.MakeInParam("@staffid", SqlDbType.Int, 4, staffId),
                        Database.MakeInParam("@begintime", SqlDbType.DateTime, 8, beginTime),
                        Database.MakeInParam("@endtime", SqlDbType.DateTime, 8, endTime)
                    };

                    DataTable table; //应该出勤的天数
                    using (SqlDataReader dr = db.RunDataReader("dbo.sproc_WA_GetAbsenceDay", prams))
                    {
                        table = Tools.ConvertDataReaderToDataTable(dr);
                    }

                    var myDataColumn = new DataColumn();
                    myDataColumn.DataType = Type.GetType("System.String");
                    myDataColumn.ColumnName = "OnDuty";
                    table.Columns.Add(myDataColumn);

                    myDataColumn = new DataColumn();
                    myDataColumn.DataType = Type.GetType("System.String");
                    myDataColumn.ColumnName = "OffDuty";
                    table.Columns.Add(myDataColumn);

                    myDataColumn = new DataColumn();
                    myDataColumn.DataType = Type.GetType("System.String");
                    myDataColumn.ColumnName = "OnDuty_MemoID";
                    table.Columns.Add(myDataColumn);

                    myDataColumn = new DataColumn();
                    myDataColumn.DataType = Type.GetType("System.String");
                    myDataColumn.ColumnName = "OffDuty_MemoID";
                    table.Columns.Add(myDataColumn);

                    table.DefaultView.Sort = "WorkDate ASC";
                    grdStaff.DataSource = table.DefaultView;
                    grdStaff.DataBind();
                }
                else
                {
                    DataTable table;
                    using (SqlDataReader dr = WA_Duty.GetStaffAttendanceData(beginTime, endTime, new[] { staffId }))
                    {
                        table = Tools.ConvertDataReaderToDataTable(dr);
                    }
                    table.DefaultView.RowFilter = filter;

                    grdStaff.DataSource = table.DefaultView;
                    grdStaff.DataBind();
                }

                ltlbegintime.Text = beginTime.ToString("yyyy-MM-dd");
                ltlendtime.Text = beginTime.ToString("yyyy-MM-dd");
                lbltype.Text = type;
            }

            base.OnLoad(e);
        }
    }
}